///***********************************************************************************************************************
// 文 件 名: Example_GPIO.c
// 功    能: 输入输出测试
//
//
//
// 作    者：zhangzj
// 创建时间: 2024.03.18
//
// 日    志：
// [2024.03.18] 创建。
//***********************************************************************************************************************/
#include "NL8507.h"

//----------------------------------------------------------------------------------------------------------------------
// 宏定义
#define OUT_POE  0x40
#define OUT_NOE  0x20
#define OUT_PU   0x10
#define OUT_PD   0x08
#define IN_EN    0x80
#define OUT_UART 0x01
#define OUT_PWM4 0x02
#define OUT_PWM2 0x03
#define CLOSE    0x0
#define OUT_PULL 0x60
#define OUT_UPOD 0x30
#define IO_P     0xE0
#define IO_UD    0xC8
//----------------------------------------------------------------------------------------------------------------------
//定义全局函数
void Port_Mod_Config (u8 mode);
void Port_Toggle(void);
void Port_OUT(u8 level);
//----------------------------------------------------------------------------------------------------------------------
// 定义全局变量
volatile u8 i = 0;
unsigned char xdata src_data0 _at_ 0x000;
unsigned char xdata src_data1 _at_ 0x001;
unsigned char xdata src_data2 _at_ 0x002;
unsigned char xdata src_data3 _at_ 0x003;
unsigned char xdata add_data0 _at_ 0x004;
unsigned char xdata add_data1 _at_ 0x005;
unsigned char xdata add_data2 _at_ 0x006;
unsigned char xdata add_data3 _at_ 0x007;

unsigned char  add_data4 _at_ 0x17;

unsigned char AES_LineMul(unsigned char sdata,unsigned char mul)
{
    unsigned char i;
    unsigned char buf = 0;

    for(i=0;i<(mul>>1);i++){
        if(sdata&0x80) buf = sdata ^ 0x1B;
        else          buf = sdata << 1  ;
    }
    if(mul & 0x01) buf = buf ^ sdata;

    return buf;
}
unsigned char AES_LineMult(unsigned char sdata,unsigned char mul)
{
    QENP_SRC_ADR_H = sdata;
    QENP_SRC_ADR_L = mul;
    QENP_ADD_ADR_H = 0x01;
    while(QENP_ADD_ADR_L&0x80);

    return QENP_SRC_ADR_L;
}
//======================================================================================================================
// 名称：main
// 功能：
//
void main(void)
{
  ////1.关狗
	WDTCN = 0x08;
	PCON0 = 0x30;
	//Port_Mod_Config(CLOSE);
  src_data0 = 0xCC;
  src_data1 = 0xCC;
  src_data2 = 0xCC;
  src_data3 = 0xCC;
  add_data0 = 0x99;
  add_data1 = 0x99;
  add_data2 = 0x99;
  add_data3 = 0x99;
  // add_data4 = 0xAB;
  // add_data4 = add_data4 ^ src_data0 ^ add_data0;

  PB2MD = 0xE0;
  PB3MD = 0xE0;
  STCON = 0x7A;
  STPSC = 0x0A;

  // AES_LineMul(src_data3,add_data2);
  // AES_LineMult(src_data3,add_data2);
  while(1)
	{
    PB2 = ~PB2;
//    PB2 = 0;
//    PB2 = 1;
    if(STCON&0x80)
    {
      STCON = 0x7A;
      PB3 = ~PB3;
      SFR_PAGE = 0x01;
      QENP_SRC_ADR_H = 0x01;
      QENP_SRC_ADR_L = 0x00;
      QENP_ADD_ADR_H = 0x01;
      QENP_ADD_ADR_L = 0x04;
      QENP_DAT_LEN   = 0x04;
      QENP_CN        = 0x80;
      while(QENP_CN&0x80);
      SFR_PAGE = 0x00;
    }

    //PBDAT = ~PBDAT;
	}

}

//======================================================================================================================
// 名称：Port_Mod_Config
// 功能：
//======================================================================================================================
void Port_Mod_Config (u8 mode)
{
  PA0MD = mode;
  PA1MD = mode;
  PA2MD = mode;
  PA3MD = mode;
  PA4MD = mode;
  PA5MD = mode;
  PA6MD = mode;
  PA7MD = mode;
  PB0MD = mode;
  PB1MD = mode;
  PB2MD = mode;
  PB3MD = mode;
  PB4MD = mode;
  PB5MD = mode;
  PB6MD = mode;
  PB7MD = mode;
}

//======================================================================================================================
// 名称：Port_Toggle
// 功能：
//======================================================================================================================
void Port_Toggle(void)
{
  PA0 = ~PA0;
  PA1 = ~PA1;
  PA2 = ~PA2;
  PA3 = ~PA3;
  PA4 = ~PA4;
  PA5 = ~PA5;
  PA6 = ~PA6;
  PA7 = ~PA7;
  PB0 = ~PB0;
  PB1 = ~PB1;
  PB2 = ~PB2;
  PB3 = ~PB3;
  PB4 = ~PB4;
  PB5 = ~PB5;
  PB6 = ~PB6;
  PB7 = ~PB7;
}

//======================================================================================================================
// 名称：Port_OUT
// 功能：
//======================================================================================================================
void Port_OUT(u8 level)
{
  PA0 = level;
  PA1 = level;
  PA2 = level;
  PA3 = level;
  PA4 = level;
  PA5 = level;
  PA6 = level;
  PA7 = level;
  PB0 = level;
  PB1 = level;
  PB2 = level;
  PB3 = level;
  PB4 = level;
  PB5 = level;
  PB6 = level;
  PB7 = level;
}